package com.gj.xxl.job.admin.core.config;

import com.gj.xxl.job.admin.core.schedule.GjJobSchedule;
import com.gj.xxl.job.admin.core.thread.JobScheduleHelper;
import com.gj.xxl.job.admin.core.thread.JobTriggerHelper;
import com.gj.xxl.job.admin.dao.XxlJobGroupDAO;
import com.gj.xxl.job.admin.dao.XxlJobInfoDAO;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * 调度中心的启动入口
 */
@Component
public class GjJobScheduleConfig implements InitializingBean, DisposableBean {

    private static GjJobScheduleConfig instance;
    public static GjJobScheduleConfig getInstance() {
        return instance;
    }

    private GjJobSchedule gjJobSchedule;

    //在 spring 容器中的 bean 初始化完成后调用，启动线程
    @Override
    public void afterPropertiesSet() throws Exception {
        instance = this;
        gjJobSchedule = new GjJobSchedule();
        // 启动调度中心服务端
        gjJobSchedule.init();

    }


    @Autowired
    private XxlJobInfoDAO xxlJobInfoDAO;
    @Autowired
    private XxlJobGroupDAO xxlJobGroupDAO;
    @Autowired
    private DataSource dataSource;


    @Value("${xxl.job.accessToken}")
    private String accessToken;


    public DataSource getDataSource() {
        return dataSource;
    }

    public XxlJobInfoDAO getJobInfoDAO() {
        return xxlJobInfoDAO;
    }

    public XxlJobGroupDAO getXxlJobGroupDAO() {
        return xxlJobGroupDAO;
    }

    public String getAccessToken() {
        return accessToken;
    }

    @Override
    public void destroy() throws Exception {
        JobScheduleHelper.getInstance().destroy();

        JobTriggerHelper.getInstance().destroy();
    }
}
